package xyz.redtorch.web.core.advice;

import io.netty.handler.codec.http.HttpResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.redtorch.web.core.exception.ParamValidateException;
import xyz.redtorch.web.core.exception.ResourcesNotFoundException;
import xyz.redtorch.web.core.vo.ResultVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class DefaultExceptionHandler {
    public DefaultExceptionHandler() {
    }

    @ExceptionHandler({ParamValidateException.class})
    public ResultVO handleParamValidateException(ParamValidateException exception, HttpServletResponse response) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return new ResultVO(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage());
    }

    @ExceptionHandler({ResourcesNotFoundException.class})
    public ResultVO handleResourcesNotFoundException(ResourcesNotFoundException exception, HttpServletResponse response) {
        response.setStatus(exception.getCode());
        return new ResultVO(exception.getCode(), exception.getMessage());
    }

//    @ExceptionHandler({MethodArgumentNotValidException.class})
//    public ResultVO<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request, HttpServletResponse response) {
//        BindingResult bindingResult = e.getBindingResult();
//        StringBuilder sb = new StringBuilder();
//        Iterator var6 = bindingResult.getFieldErrors().iterator();
//        if (var6.hasNext()) {
//            FieldError fieldError = (FieldError)var6.next();
//            sb.append(fieldError.getDefaultMessage());
//        }
//
//        return this.iCityException(new ICityException(HttpStatus.BAD_REQUEST.value(), 999, sb.toString()), request, response);
//    }
//
//    @ExceptionHandler({NestedRuntimeException.class})
//    public ResultVO<Void> springException(NestedRuntimeException exception, HttpServletRequest request, HttpServletResponse response) {
//        return this.handle(HttpStatus.BAD_REQUEST, 997, exception, request, response);
//    }
//
//    @ExceptionHandler({ServletException.class})
//    public ResultVO<Void> servletException(ServletException exception, HttpServletRequest request, HttpServletResponse response) {
//        return this.handle(HttpStatus.BAD_REQUEST, 998, exception, request, response);
//    }
//
//    @ExceptionHandler({Exception.class})
//    public ResultVO<Void> defaultErrorHandler(Exception exception, HttpServletRequest request, HttpServletResponse response) {
//        return this.handle(HttpStatus.INTERNAL_SERVER_ERROR, 999, exception, request, response);
//    }
//
//    protected ResultVO<Void> handle(HttpStatus status, int code, Throwable cause, HttpServletRequest request, HttpServletResponse response) {
//        if (StringUtils.hasText(response.getHeader("Content-Disposition"))) {
//            response.setStatus(status.value());
//        } else {
//            response.setStatus(HttpStatus.OK.value());
//        }
//
//        cause.printStackTrace();
//        response.setHeader("Code", "" + status.value() + code);
//
//        try {
//            response.setHeader("Message", URLEncoder.encode(cause.getMessage() == null ? "null" : cause.getMessage(), "UTF-8"));
//        } catch (UnsupportedEncodingException var8) {
//        }
//
//        GenericResult<Void> error = new GenericResult(Integer.valueOf(status.value() + "" + code), cause != null && cause.getMessage() != null ? cause.getMessage() : "null");
//        String url = request.getRequestURI() + (StringUtils.isEmpty(request.getQueryString()) ? "" : "?" + request.getQueryString());
//        System.out.println("[request] [" + request.getMethod() + "]" + url);
//        System.out.println("[response] " + error);
//        return error;
//    }
}
